package com.LinSoft.EquationSolver;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.View.*;


public class x1solve extends Activity
{
	EditText ce_a, ce_b, ce_c;
	TextView ct_res;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.l_x1solve);
		Button cb_solve = (Button)findViewById(R.id.xb_x1_solve);
		cb_solve.setOnClickListener(new OnClickListener()
			{
				public void onClick(View v)
				{
					solve();
				}
			});
		ce_a = (EditText)findViewById(R.id.xe_x1_a);
		ce_b = (EditText)findViewById(R.id.xe_x1_b);
		ce_c = (EditText)findViewById(R.id.xe_x1_c);
		ct_res = (TextView)findViewById(R.id.xt_x1_res);
/*		ce_a.setOnKeyListener(new OnKeyListener()
			{
				public boolean onKey(View v, int keyCode, KeyEvent event)
				{
					if(keyCode == KeyEvent.KEYCODE_ENTER
						&& event.getAction() == KeyEvent.ACTION_DOWN)
					{
						ce_b.requestFocus();
						return true;
					}
					return false;
				}
			});
		ce_b.setOnKeyListener(new OnKeyListener()
			{
				public boolean onKey(View v, int keyCode, KeyEvent event)
				{
					if(keyCode == KeyEvent.KEYCODE_ENTER
						&& event.getAction() == KeyEvent.ACTION_DOWN)
					{
						ce_c.requestFocus();
						return true;
					}
					return false;
				}
			});*/
		ce_c.setOnKeyListener(new OnKeyListener()
			{
				public boolean onKey(View v, int keyCode, KeyEvent event)
				{
					if(keyCode == KeyEvent.KEYCODE_ENTER
						&& event.getAction() == KeyEvent.ACTION_DOWN)
					{
						solve();
						return true;
					}
					return false;
				}
			});
	}
	
	protected void solve()
	{
		int a, b, c, d, t;
		boolean neg = false;
		a = Integer.parseInt(ce_a.getText().toString());
		b = Integer.parseInt(ce_b.getText().toString());
		c = Integer.parseInt(ce_c.getText().toString());
		if(a == 0)
		{
			ct_res.setText(R.string.err_x1_a0);
			return ;
		}
		if(a * (c - b) < 0)
			neg = true;
		t = gcd(a = Math.abs(a), d = Math.abs(c - b));
		a /= t;
		d /= t;
		String s;
		s = "X = ";
		if(neg)
			s += "-";
		if(a == 1)
			s += d;
		else
			s += "("+ d + ")/(" + a + ")";
		ct_res.setText(s);
		ce_a.requestFocus();
	}
	
	protected int gcd(int a, int b)
	{
		if(b == 0)
			return a;
		return gcd(b, a % b);
	}
}
